<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <title>后台管理</title>
    <link rel="stylesheet" type="text/css" href="css/common.css"/>
    <link rel="stylesheet" type="text/css" href="css/main.css"/>
	<script type="text/javascript" src="js/public.js"></script>
    <script>
        window.onload = function (){
			var token = sessionStorage.getItem("token");
			if(token == null){
				window.location.href = "/shop/login.html";
			}
			
			var consumerName = sessionStorage.getItem("consumerName");
			var role = "店长";
			if(sessionStorage.getItem("role") > 1){
							  role = "店员";
			}
			document.getElementById("manager_name").innerText = role+":"+consumerName;
			
            fetch("http://localhost:8080/order/listOrderDetailStore",{method: 'GET',  
				headers: {  
					'token': sessionStorage.getItem("token"), 
					},  
				})
			
                .then(response => {
                    // 确保服务器返回的是JSON数据
                    if (!response.ok) {
                        throw new Error('Failed to fetch data');
                    }
                    return response.json(); // 将response转换为JSON
                })
                .then(data => {
					if(data==null){
						return;
					}
					str = "";
                    for(var i=0;i<data.length;i++) {
						str += "<tr id=\"order_"+data[i].detailId+"\">";
                        str += "<td >" + data[i].order.orderNumber + "</td>";
						str += "<td >" + data[i].order.orderPlacedTime + "</td>";
						str += "<td >" + data[i].phone + "</td>";
						str += "<td >" + data[i].order.deliveryAddress+ "</td>";
						
						str += "<td >" + data[i].product.productName + "</td>";
						str += "<td >" + data[i].purchaseQuantity + "</td>";
						str += "<td >" + data[i].unitPrice + "</td>";
						if (data[i].order.orderStatus == 0){
							str += "<td colspan = 2></td>";
							str += "<td>未支付</td>";
						}else if(data[i].order.orderStatus == 1){
							str += "<td>"+data[i].order.paymentMethod+"</td>";
							str += "<td>"+data[i].order.paymentCompletedTime+"</td>";
							str += "<td>已支付</td>";
							str += "<td><input type=\"button\" value=\"退单\" onclick=\"chargeback("+data[i].detailId+")\"/><input type=\"button\" value=\"发货\" onclick=\"delivery("+data[i].detailId+")\"/></td>";
						}else if(data[i].order.orderStatus == 2){
							str += "<td>"+data[i].order.paymentMethod+"</td>";
							str += "<td>"+data[i].order.paymentCompletedTime+"</td>";
							str += "<td>已发货</td>";
							str += "<td><input type=\"button\" value=\"退单\" onclick=\"chargeback("+data[i].detailId+")\"/>";
						}else{
							str += "<td>"+data[i].order.paymentMethod+"</td>";
							str += "<td>"+data[i].order.paymentCompletedTime+"</td>";
							str += "<td>已退单</td>";
							str += "<td/>";
						}
						str += "</tr>";
                    }
                    document.getElementById("all_order").innerHTML = str;
                })
                .catch(error => {
                    // 处理错误
					console.error('Error:', error);
                });
        }
		
		function Chargeback(mid){
			fetch("http://localhost:8080/product/delete/"+mid+"",{method: 'DELETE',
				headers: {  
					'token': sessionStorage.getItem("token"), 
					},  
				})
			
			    .then(response => {
			        // 确保服务器返回的是JSON数据
			        if (!response.ok) {
			            throw new Error('Failed to fetch data');
			        }
			    })
			    .then(data => {
			        alert("删除成功");
					location.href = location.href; 
			    })
			    .catch(error => {
					console.error('Error:', error);
			    });
		}
		
		function update_product(mid){
			var aproducts = sessionStorage.getItem("products");
			let products = JSON.parse(aproducts); 
			for(var i=0;i<products.length;i++){
				let amid = mid.toString(); 
				if (products[i].productId == amid){
					str="";
					str += "<tr id=\"product_"+products[i].productId+"\">";
					str += "<td colspan=2><input type=\"text\" id=\"productName_"+products[i].productId+"\" value=\""+ products[i].productName +"\"></td>";
					str += "<td ><input type=\"text\" id=\"price_"+products[i].productId+"\" value=\""+ products[i].price +"\"></td>";
					str += "<td ><input type=\"text\" id=\"stockQuantity_"+products[i].productId+"\" value=\""+ products[i].stockQuantity +"\"></td>";
					str += "<td ><input type=\"text\" id=\"description_"+products[i].productId+"\" value=\""+ products[i].description +"\"></td>";
					str += "<td ><input type=\"text\" id=\"imageUrl_"+products[i].productId+"\" value=\""+ products[i].imageUrl +"\"></td>";
					str += "<td colspan=2><select name=\"isOnSale\" id=\"isOnSale_"+products[i].productId+"\" class=\"required\">";
                    str += "<option value=\"\">请选择</option>";
					str += "<option value=\"1\">上架</option>";
					str += "<option value=\"0\">下架</option>";
                    str += "</select></td>";
					str += "<td><input type=\"button\" value=\"确认\" onclick=\"update("+products[i].productId+")\"></td>";
					str += "</tr>";
					document.getElementById("product_"+mid).innerHTML = str;
				}
			}
		}

    </script>
</head>
<body>
<div class="topbar-wrap white">
    <div class="topbar-inner clearfix">
        <div class="topbar-logo-wrap clearfix">
            <h1 class="topbar-logo none"><a href="admin.html" class="navbar-brand">后台管理</a></h1>
            <ul class="navbar-list clearfix">
                <li><a class="on" href="admin.html">首页</a></li>
                <li><a href="admin.html" target="_blank">网站首页</a></li>
            </ul>
        </div>
        <div class="top-info-wrap">
            <ul class="top-info-list clearfix">
                <li><a href="store.html" id= "manager_name">管理员</a></li>
                <li><a href="password.html">修改密码</a></li>
                <li><a href="#" onclick="logout()">退出</a></li>
            </ul>
        </div>
    </div>
</div>
<div class="container clearfix">
    <div class="sidebar-wrap">
        <div class="sidebar-title">
            <h1>菜单</h1>
        </div>
        <div class="sidebar-content">
            <ul class="sidebar-list">
                <li>
                    <a href="#"><i class="icon-font">&#xe003;</i>常用操作</a>
                    <ul class="sub-menu">
                        <li><a href="product.html"><i class="icon-font">&#xe008;</i>商品管理</a></li>
                        <li><a href="order.html"><i class="icon-font">&#xe005;</i>订单管理</a></li>
						<li><a href="insert.html"><i class="icon-font">&#xe005;</i>商品新增</a></li>
						<li><a href="category.html"><i class="icon-font">&#xe005;</i>类型管理</a></li>
                    </ul>
                </li>
                <li>
                    <a href="#"><i class="icon-font">&#xe018;</i>系统管理</a>
                    <ul class="sub-menu">
                        <li><a href="store.html"><i class="icon-font">&#xe017;</i>店铺信息设置</a></li>
						<li><a href="manager.html"><i class="icon-font">&#xe017;</i>个人信息设置</a></li>
						<li><a href="members.html"><i class="icon-font">&#xe017;</i>店铺成员信息</a></li>
                    </ul>
                </li>
            </ul>
        </div>
    </div>
    <!--/sidebar-->
    <div class="main-wrap">

        <div class="crumb-wrap">
            <div class="crumb-list"><i class="icon-font"></i><a href="admin.html">首页</a><span class="crumb-step">&gt;</span><a class="crumb-name" href="/jscss/admin/design/">订单管理</a><span class="crumb-step">&gt;</span><span>订单列表</span></div>
        </div>
        <div class="result-wrap">
            <div class="result-content">
                    <table class="insert-tab" width="100%">
						<thead>
							<td><i>订单编号</i></td>
							<td><i>下单时间</i></td>
							<td><i>用户手机号</i></td>
							<td><i>地址</i></td>
							<td><i>商品名</i></td>
							<td><i>数量</i></td>
							<td><i>购买时单价</i></td>
							<td><i>支付方式</i></td>
							<td><i>支付时间</i></td>
							<td><i>状态</i></td>
							<td><i>操作</i></td>
						</thead>
                        <tbody id="all_order">
							<tr>
								<td colspan = 5 style="text-align: center;">无订单</td>
							</tr>
                        </tbody></table>
            </div>
			<div class="pagination">
			    <a href="#" class="page-item">首页</a>  
			    <a href="#" class="page-item disabled">上一页</a>  
			      
			    <!-- 假设当前是第3页，所以显示第1-5页 -->  
			    <a href="#" class="page-item active">1</a>  
			    <a href="#" class="page-item">2</a>  
			    <a href="#" class="page-item">3</a>  
			    <a href="#" class="page-item">4</a>  
			    <a href="#" class="page-item">5</a>  
			      
			    <a href="#" class="page-item">下一页</a>  
			    <a href="#" class="page-item">尾页</a>  
			</div>
        </div>

    </div>
    <!--/main-->
</div>
</body>
</html>